package com.after.saas.file.utils;

import cn.hutool.core.date.DatePattern;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.io.FileUtil;
import cn.hutool.core.util.IdUtil;
import cn.hutool.core.util.ZipUtil;
import lombok.extern.slf4j.Slf4j;

import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.nio.charset.StandardCharsets;
import java.text.DecimalFormat;
import java.time.LocalDateTime;
import java.util.Arrays;
import java.util.Calendar;
import java.util.List;
import java.util.zip.ZipOutputStream;

/**
 * @Description 字节流压缩文件
 * @Classname CustomerZipUtil
 * @Author hjchen
 * @Date 2023/7/13 16:38
 */

@Slf4j
public class CustomerZipUtil {


    public static void main(String[] args) {
        String path = "D:\\test\\zipTest";
        byte[][] bytes = new byte[1000][];
        String[][] arrays = getStringList(1000);
        for (int i = 0; i < arrays[0].length; i++) {
            bytes[i] = arrays[0][i].getBytes(StandardCharsets.UTF_8);
        }
        long start = System.currentTimeMillis();
        compressContent(Arrays.asList(arrays[0]), arrays[1], generatePath(path));
        System.out.println("string compressContent =" + (System.currentTimeMillis() - start));
        saveContentFile(Arrays.asList(arrays[0]), path);
        long now = System.currentTimeMillis();
        compressContent(bytes, arrays[1], generatePath(path));
        System.out.println("byte compressContent =" + (System.currentTimeMillis() - now));
    }

    private static String[][] getStringList(int size) {
        String[][] arrays = new String[2][];
        String[] arrayContent = new String[size];
        String[] arrayName = new String[size];
        for (int i = 0; i < size; i++) {
            arrayContent[i] = "test content" + i;
            arrayName[i] = "test" + i + ".txt";
        }
        arrays[0] = arrayContent;
        arrays[1] = arrayName;
        return arrays;
    }

    /**
     * 压缩字节
     *
     * @param bytesArr
     * @param array
     * @param path
     * @return void
     * @author hjchen
     * @date 2023/7/17 10:01
     **/
    public static void compressContent(byte[][] bytesArr, String[] array, String path) {
        ByteArrayInputStream[] byteArrayInputStreams = new ByteArrayInputStream[array.length];
        try {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path));
            for (int i = 0; i < bytesArr.length; i++) {
                byteArrayInputStreams[i] = new ByteArrayInputStream(bytesArr[i]);
            }
            ZipUtil.zip(zos, array, byteArrayInputStreams);
        } catch (Exception e) {
            log.error("备份文件压缩失败：", e);
        }
    }

    /**
     * 压缩字符
     *
     * @param contents
     * @param array
     * @param path
     * @return void
     * @author hjchen
     * @date 2023/7/17 10:01
     **/
    public static void compressContent(List<String> contents, String[] array, String path) {
        ByteArrayInputStream[] byteArrayInputStreams = new ByteArrayInputStream[array.length];
        try {
            ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(path));
            for (int i = 0; i < contents.size(); i++) {
                byteArrayInputStreams[i] = new ByteArrayInputStream(contents.get(i).getBytes(StandardCharsets.UTF_8));
            }
            ZipUtil.zip(zos, array, byteArrayInputStreams);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    public static void saveContentFile(List<String> contents, String path) {
        FileUtil.mkdir(path);
        long now = System.currentTimeMillis();
        for (String s : contents) {
            FileUtil.writeUtf8String(s, String.format("%s\\%s.txt", path, IdUtil.fastSimpleUUID()));
        }
        System.out.println("stringToFile compressContent =" + (System.currentTimeMillis() - now));
    }

    public static String generatePath(String path) {
        //显示两位，不足位则补0
        DecimalFormat df = new DecimalFormat("#00");
        Calendar cal = Calendar.getInstance();
        //目录规则：当前年/月/日/时/分
        String filePath = path + File.separator + cal.get(Calendar.YEAR) + File.separator + df.format(cal.get(Calendar.MONTH) + 1) + File.separator + df.format(cal.get(Calendar.DATE)) + File.separator + df.format(cal.get(Calendar.HOUR_OF_DAY)) + File.separator + df.format(cal.get(Calendar.MINUTE));
        FileUtil.mkdir(filePath);
        filePath += File.separator + String.format("%s_%s.zip", DateUtil.format(LocalDateTime.now(), DatePattern.PURE_DATETIME_PATTERN), IdUtil.fastSimpleUUID());
        return filePath;
    }


}
